package org.iori.luodan.logic;

import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.iori.luodan.base.BaseDaoFactory;
import org.iori.luodan.dao.GoodsDao;
import org.iori.luodan.entity.GoodsEntity;

import com.iori.common.log.FounderLog;
import com.iori.common.log.LogFactory;

/**
 * [クラス名]<br>
 * GoodsLogic.java<br>
 * [機能概要]<br>
 * 検索ロジック<br>
 * <br>
 * [変更履歴]<br>
 * 2012/10/19 ver1.00 新規作成<br>
 * 
 * @author luodan CORPORATION lun_xing
 * @version 1.00
 */
public class GoodsLogic {

    // Log出力クラスを定義
    private static FounderLog log = LogFactory.getFounderLog(GoodsLogic.class);

    // DAO初期化
    private static GoodsDao goodsDao = BaseDaoFactory.getGoodsDao();

    /**
     * コンテンツの内容取得
     * <br>
     * @param offset
     * @param limit
     * @param TitleName
     * @return
     * @throws Exception
     */
    public GoodsEntity getGoods(int goods_id) throws Exception {
    	HashMap map = goodsDao.getGoods(goods_id);

    	GoodsEntity goodsEntity = this.mapToEntity(map);

		return goodsEntity;
    }
    
    /**
     * コンテンツリストの内容取得
     * <br>
     * @param offset
     * @param limit
     * @param TitleName
     * @return
     * @throws Exception
     */
    public ArrayList getGoodsList(int cat_id, int start_row, int row_count) throws Exception {
		List goodsList = null;
		ArrayList goodsEntityList = new ArrayList();
		goodsList = goodsDao.getGoodsList(cat_id, start_row, row_count);
		if (goodsList != null) {
		    log.debug("goodsListのサイズ：" + goodsList.size());
		}
	
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String currentTime = sdf.format(new java.util.Date());
	
		if (goodsList != null && goodsList.size() > 0) {
			for (int i = 0; i < goodsList.size(); i++) {
				HashMap map = (HashMap)goodsList.get(i);
				GoodsEntity goodsEntity = this.mapToEntity(map);
				if (null != goodsEntity) {
					goodsEntityList.add(goodsEntity);
				}
		    }
		}
		return goodsEntityList;
    }
    
    private GoodsEntity mapToEntity(HashMap map) {
    	if (null == map) {
    		return null;
    	}
    	
    	Object obj = null;
    	obj = map.get("GOODS_ID");
    	int goods_id = 0;
    	if (null != obj) {
    		goods_id = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("CAT_ID");
    	int cat_id = 0;
    	if (null != obj) {
    		cat_id = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("GOODS_SN");
    	String goods_sn = "";
    	if (null != obj) {
    		goods_sn = obj.toString().trim();
    	}
    	
    	obj = map.get("GOODS_NAME");
    	String goods_name = "";
    	if (null != obj) {
    		goods_name = obj.toString().trim();
    	}
    	
    	obj = map.get("GOODS_NAME_STYLE");
    	String goods_name_style = "";
    	if (null != obj) {
    		goods_name_style = obj.toString().trim();
    	}
    	
    	obj = map.get("CLICK_COUNT");
    	int click_count = 0;
    	if (null != obj) {
    		click_count = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("BRAND_ID");
    	int brand_id = 0;
    	if (null != obj) {
    		brand_id = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("PROVIDER_NAME");
    	String provider_name = "";
    	if (null != obj) {
    		provider_name = obj.toString().trim();
    	}
    	
    	obj = map.get("GOODS_NUMBER");
    	int goods_number = 0;
    	if (null != obj) {
    		goods_number = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("GOODS_WEIGHT");
    	float goods_weight = 0.0f;
    	if (null != obj) {
    		goods_weight = Float.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("MARKET_PRICE");
    	float market_price = 0.0f;
    	if (null != obj) {
    		market_price = Float.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("SHOP_PRICE");
    	float shop_price = 0.0f;
    	if (null != obj) {
    		shop_price = Float.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("PROMOTE_PRICE");
    	float promote_price = 0.0f;
    	if (null != obj) {
    		promote_price = Float.valueOf(obj.toString().trim());
    	}

    	obj = map.get("PROMOTE_START_DATE");
    	int promote_start_date = 0;
    	if (null != obj) {
    		promote_start_date = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("PROMOTE_END_DATE");
    	int promote_end_date = 0;
    	if (null != obj) {
    		promote_end_date = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("WARN_NUMBER");
    	int warn_number = 0;
    	if (null != obj) {
    		warn_number = Integer.valueOf(obj.toString().trim());
    	}

    	obj = map.get("KEYWORDS");
    	String keywords = "";
    	if (null != obj) {
    		keywords = obj.toString().trim();
    	}
    	
    	obj = map.get("GOODS_BRIEF");
    	String goods_brief = "";
    	if (null != obj) {
    		goods_brief = obj.toString().trim();
    	}
    	
    	obj = map.get("GOODS_DESC");
    	String goods_desc = "";
    	if (null != obj) {
    		goods_desc = obj.toString().trim();
    	}
    	
    	obj = map.get("GOODS_THUMB");
    	String goods_thumb = "";
    	if (null != obj) {
    		goods_thumb = obj.toString().trim();
    	}
    	
    	obj = map.get("GOODS_IMG");
    	String goods_img = "";
    	if (null != obj) {
    		goods_img = obj.toString().trim();
    	}
    	
    	obj = map.get("ORIGINAL_IMG");
    	String original_img = "";
    	if (null != obj) {
    		original_img = obj.toString().trim();
    	}

    	obj = map.get("IS_REAL");
    	int is_real = 0;
    	if (null != obj) {
    		is_real = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("EXTENSION_CODE");
    	String extension_code = "";
    	if (null != obj) {
    		extension_code = obj.toString().trim();
    	}

       	obj = map.get("IS_ON_SALE");
       	Boolean is_on_sale = false;
    	if (null != obj) {
    		is_on_sale = Boolean.valueOf(obj.toString().trim());
    	}
    	
       	obj = map.get("IS_ALONE_SALE");
       	Boolean is_alone_sale = false;
    	if (null != obj) {
    		is_alone_sale = Boolean.valueOf(obj.toString().trim());
    	}
    	
       	obj = map.get("IS_SHIPPING");
       	Boolean is_shipping = false;
    	if (null != obj) {
    		is_shipping = Boolean.valueOf(obj.toString().trim());
    	}

    	obj = map.get("INTEGRAL");
    	int integral = 0;
    	if (null != obj) {
    		integral = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("ADD_TIME");
    	int add_time = 0;
    	if (null != obj) {
    		add_time = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("SORT_ORDER");
    	int sort_order = 0;
    	if (null != obj) {
    		sort_order = Integer.valueOf(obj.toString().trim());
    	}

       	obj = map.get("IS_DELETE");
       	Boolean is_delete = false;
    	if (null != obj) {
    		is_delete = Boolean.valueOf(obj.toString().trim());
    	}
    	
       	obj = map.get("IS_BEST");
       	Boolean is_best = false;
    	if (null != obj) {
    		is_best = Boolean.valueOf(obj.toString().trim());
    	}
    	
       	obj = map.get("IS_NEW");
       	Boolean is_new = false;
    	if (null != obj) {
    		is_new = Boolean.valueOf(obj.toString().trim());
    	}
    	
       	obj = map.get("IS_HOT");
       	Boolean is_hot = false;
    	if (null != obj) {
    		is_hot = Boolean.valueOf(obj.toString().trim());
    	}
    	
       	obj = map.get("IS_PROMOTE");
       	Boolean is_promote = false;
    	if (null != obj) {
    		is_promote = Boolean.valueOf(obj.toString().trim());
    	}

    	obj = map.get("BONUS_TYPE_ID");
    	int bonus_type_id = 0;
    	if (null != obj) {
    		bonus_type_id = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("LAST_UPDATE");
    	int last_update = 0;
    	if (null != obj) {
    		last_update = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("GOODS_TYPE");
    	int goods_type = 0;
    	if (null != obj) {
    		goods_type = Integer.valueOf(obj.toString().trim());
    	}

    	obj = map.get("SELLER_NOTE");
    	String seller_note = "";
    	if (null != obj) {
    		seller_note = obj.toString().trim();
    	}

    	obj = map.get("GIVE_INTEGRAL");
    	int give_integral = 0;
    	if (null != obj) {
    		give_integral = Integer.valueOf(obj.toString().trim());
    	}
    	obj = map.get("RANK_INTEGRAL");
    	int rank_integral = 0;
    	if (null != obj) {
    		rank_integral = Integer.valueOf(obj.toString().trim());
    	}
    	obj = map.get("SUPPLIERS_ID");
    	int suppliers_id = 0;
    	if (null != obj) {
    		suppliers_id = Integer.valueOf(obj.toString().trim());
    	}




		obj = map.get("IS_CHECK");
		Boolean is_check = false;
		if (null != obj) {
			is_check = Boolean.valueOf(obj.toString().trim());
		}
		
		obj = map.get("COMMENT_RANK");
		int comment_rank = 0;
		if (null != obj) {
			comment_rank = Integer.valueOf(obj.toString().trim());
		}

		GoodsEntity goodsEntity = new GoodsEntity();

		goodsEntity.setGoods_id(goods_id);
		goodsEntity.setCat_id(cat_id);
		goodsEntity.setGoods_sn(goods_sn);
		goodsEntity.setGoods_name(goods_name);
		goodsEntity.setGoods_name_style(goods_name_style);
		goodsEntity.setClick_count(click_count);
		goodsEntity.setBrand_id(brand_id);
		goodsEntity.setProvider_name(provider_name);
		goodsEntity.setGoods_number(goods_number);
		goodsEntity.setGoods_weight(goods_weight);
		goodsEntity.setMarket_price(market_price);
		goodsEntity.setShop_price(shop_price);
		goodsEntity.setPromote_price(promote_price);
		goodsEntity.setPromote_start_date(promote_start_date);
		goodsEntity.setPromote_end_date(promote_end_date);
		goodsEntity.setWarn_number(warn_number);
		goodsEntity.setKeywords(keywords);
		goodsEntity.setGoods_brief(goods_brief);
		goodsEntity.setGoods_desc(goods_desc);
		goodsEntity.setGoods_thumb(goods_thumb);
		goodsEntity.setGoods_img(goods_img);
		goodsEntity.setOriginal_img(original_img);
		goodsEntity.setIs_real(is_real);
		goodsEntity.setExtension_code(extension_code);
		goodsEntity.setIs_on_sale(is_on_sale);
		goodsEntity.setIs_alone_sale(is_alone_sale);
		goodsEntity.setIs_shipping(is_shipping);
		goodsEntity.setIntegral(integral);
		goodsEntity.setAdd_time(add_time);
		goodsEntity.setSort_order(sort_order);
		goodsEntity.setIs_delete(is_delete);
		goodsEntity.setIs_best(is_best);
		goodsEntity.setIs_new(is_new);
		goodsEntity.setIs_hot(is_hot);
		goodsEntity.setIs_promote(is_promote);
		goodsEntity.setBonus_type_id(bonus_type_id);
		goodsEntity.setLast_update(last_update);
		goodsEntity.setGoods_type(goods_type);
		goodsEntity.setSeller_note(seller_note);
		goodsEntity.setGive_integral(give_integral);
		goodsEntity.setRank_integral(rank_integral);
		goodsEntity.setSuppliers_id(suppliers_id);
		goodsEntity.setIs_check(is_check);

		String commentRank_img = "themes/default/images/stars"+ String.valueOf(comment_rank) + ".gif";
		goodsEntity.setComment_rank_img(commentRank_img);

    	return goodsEntity;
    }

    /**
     * 該当商品RANKの取得
     * <br>
     * @param goods_id
     * @return rankNum
     * @throws Exception
     */
    public int getRankNum(int goods_id) throws Exception {
    	int rankNum = 0;
		int salesCount = goodsDao.getSalesCount(goods_id);
		if (salesCount > 0) {
			rankNum = goodsDao.getRankNum(salesCount);
		}
		return rankNum;
    }

}
